package de.koller.renderEngine.physics;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.World;

public class PhysicsRoot implements Iterable<CubeData> {

	private final World world = new World( new Vec2( 0, 9.81f ), true );
	
	public PhysicsRoot() {
		
	}
	
	public final void update() {
		world.step( 1f/60f, 20, 20 );
		world.clearForces();
	}

	public World getWorld() {
		return world;
	}
	
	@Override
	public Iterator<CubeData> iterator() {
		return new CBIterator();
	}
	
	private class CBIterator implements Iterator<CubeData> {
		
		private Body current = null;

		private CBIterator() {
			current = world.getBodyList();
		}
	
		@Override
		public boolean hasNext() {
			return current != null;
		}

		@Override
		public CubeData next() {
			float x = current.getPosition().x;
			float y = current.getPosition().y;
			Cube c = (Cube)current.getUserData();
			float w = c.width;
			float h = c.height;
			float r = (float)Math.toDegrees( current.getAngle() );
			
			current = current.getNext();
			return new CubeData( x, y, w, h, r );
		}

		@Override
		public void remove() {
			
		}
		
	}
	
}
